package com.cjg.useClass;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class fuck {
    public static void main(String[] args) {
        lock lock = new lock();
        //写入
        for (int j = 0; j < 5; j++) {
            final int temp = 1;
            new Thread(()->{
                lock.put(temp+"",temp+"");
            },String.valueOf(j)).start();
        }

        //读取
        for (int j = 0; j < 5; j++) {
            final int temp = 1;
            new Thread(()->{
                lock.get(temp+"");
            },String.valueOf(j)).start();
        }



    }
}
class lock{
    private volatile Map<String, Object> map = new HashMap<String,Object>();
    //读写锁
    private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();


    public void put(String i,Object j){
        readWriteLock.writeLock().lock();
        try {
            System.out.println(Thread.currentThread().getName()+"写入");
            map.put(i,j);
            System.out.println(Thread.currentThread().getName()+"写入OK");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }
    public void get(String i){
        readWriteLock.readLock().lock();
        try {
            System.out.println(Thread.currentThread().getName()+"chu");
            map.get(i);
            System.out.println(Thread.currentThread().getName()+"chu ok");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readWriteLock.readLock().unlock();
        }
    }


}